#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   75575-yum-web.sh
# Version:    1.0
# Date:       2013/12/04
# Author:     yuanhui.shi
# Email:      yuanhui.shi@cs2c.com.cn
# Summary:    02系统基本功能-07开发环境-02开发工具-06yum包管理-03使用web服务器布置yum源
# Notes:      yum命令
# Copyright:  China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/12/04
#             -   The first one
# ----------------------------------------------------------------------

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

source ../../lib/Echo.sh
source ${SFROOT}/lib/NetOps.sh
source ../../lib/XmlParse.sh


function CleanData ()
{
  StopDaemon httpd

  mount | grep ${MOUNTDIR} && umount -f ${MOUNTDIR}
  # restore repo file
  for i in `ls ${YUMCONF}`
  do
      local m=`echo ${i%%.old*}`
      [ -f ${YUMCONF}/${m}.old ] && mv ${YUMCONF}/${m}.old ${YUMCONF}/${m}
  done

  rm -rf ${YUMCONF}/ns-server.repo
  EchoInfo "75575-yum-web.sh执行完毕"
}

trap "CleanData" EXIT INT

# check httpd is installed
command -v httpd > /dev/null
EchoResult "httpd exists"

# check /var/www/html is mounted
MOUNTDIR="/var/www/html"
mount | grep ${MOUNTDIR}
[ $? -eq 0 ] && umount -f ${MOUNTDIR}

EchoInfo "创建web-yum源"

mount /dev/sr0 ${MOUNTDIR} &>/dev/null
if [ $? -eq 0 ];then
    echo "PASS: moumt /dev/sr0 /var/ftp/pub"
else
    echo "FAIL: please check whether you have cdrom"
    exit 1
fi

# check mounted
mount | grep ${MOUNTDIR}
EchoResult "mount | grep ${MOUNTDIR}"

# back old repo file
YUMCONF=/etc/yum.repos.d

for i in `ls ${YUMCONF}`
do
    mv ${YUMCONF}/$i ${YUMCONF}/$i.old
done


# create new repo
cat <<-EOF >${YUMCONF}/ns-server.repo 
[ns-server]
name=NeoKylin Linux Advanced Server - Os
baseurl=http://127.0.0.1/
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-neokylin-release
enabled=1
EOF

#开启ftp服务
RestartDaemon httpd
sleep 20
netstat -ntlp | grep httpd
EchoResult "httpd start"

yum clean all
EchoResult "yum clean all"
yum list &>/dev/null
EchoResult "列出所有可安装的软件包"

! yum repolist | grep "repolist: 0"
EchoResult "通过yum repolist可以查看到ns-server yum源"
